package com.hiwan.system.service.parameter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.framework.cache.CacheService;
import org.framework.session.SessionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hiwan.system.cache.SysCacheService;
import com.hiwan.system.dao.parameter.ParameterDao;
import com.hiwan.system.pojo.DictEntry;
import com.hiwan.system.pojo.SysDictEntry;
import com.hiwan.system.pojo.ParameterInfo;
import com.hiwan.system.service.org.OrgService;
import com.hiwan.system.util.DictCacheUtils;
import com.hiwan.system.util.UserCacheUtils;

@Service
@Transactional
public class ParameterService {
	@Autowired
	private ParameterDao parameterDao;
	@Autowired
	private CacheService cacheService;
	@Autowired
	private SysCacheService sysCacheService;
	@Autowired
	private OrgService orgService;

	/**
	 * 查询字典信息表
	 * 
	 * @param dictType
	 *            字典类型
	 * @param orgId
	 *            机构号
	 * @param langCode
	 *            语言标识
	 * @param dictCode
	 *            字典编码
	 * @return
	 */
	public List<SysDictEntry> selectDictEntry(String dictType, String orgId,
			String langCode, String dictCode) {
		return parameterDao
				.selectDictEntry(dictType, orgId, langCode, dictCode);
	}
	
	/**
	 * 查询小类列表
	 * 
	 * @param paramType
	 *            参数标识
	 * @param orgId
	 *            机构号
	 * @param paramKey
	 *            主键
	 * @return
	 */
	public List<ParameterInfo> selectSubParamList(String paramType) {
		return parameterDao.selectSubParamList(paramType);
	}

	/**
	 * 获取参数字段显示名称
	 * 
	 * @param dictType
	 *            字典类型
	 * @param orgId
	 *            机构号
	 * @param langCode
	 *            语言标识
	 * @param dictCode
	 *            字典编码
	 * @return
	 */
	public List<SysDictEntry> selectDictEntryParam(String dictType,
			String orgId, String langCode, String paramType) {
		return parameterDao.selectDictEntryParam(dictType, orgId, langCode,
				paramType);
	}

	/**
	 * 查询参数详细信息
	 * 
	 * @return
	 */
	public ParameterInfo selectParamInfo(String paramType, String orgId,
			String paramKey) {
		return parameterDao.selectParamInfo(paramType, orgId, paramKey);
	}

	// 添加参数信息
	public int insertParamInfo(ParameterInfo paramInfo) {
		int ret = parameterDao.insertParamInfo(paramInfo);
		if (ret > 0) {
			updateCache(paramInfo);
			sysCacheService.reset();
		}
		return ret;
	}

	// 修改参数信息
	public int updateParamInfo(ParameterInfo paramInfo) {
		int ret = parameterDao.updateParamInfo(paramInfo);
		if (ret > 0) {
			updateCache(paramInfo);
			sysCacheService.reset();
		}
		return ret;
	}

	/**
	 * 删除参数信息
	 * 
	 * @param paramType
	 *            参数类型
	 * @param orgId
	 *            机构号
	 * @param paramKey
	 *            参数主键
	 * @return
	 */
	public int deleteParamInfo(String paramType, String orgId, String paramKey) {
		int ret = parameterDao.deleteParamInfo(paramType, orgId, paramKey);
		if (ret > 0) {
			List<ParameterInfo> paramInfoList = selectSubParamList(paramType);
			for (ParameterInfo info : paramInfoList) {
				updateCache(info);
			}
			sysCacheService.reset();
		}
		return ret;
	}

	/**
	 * 更新缓存
	 * 
	 * @param paramInfo
	 */
	private void updateCache(ParameterInfo paramInfo) {
		cacheService.put("sysCache",
				paramInfo.getParamType() + "|" + paramInfo.getOrgId() + "|"
						+ paramInfo.getParamKey(), paramInfo);
	}

	public Map<String, Object> getParamColumnNames(String paramType) {
		String[] columns = new String[] { "param1", "param2", "param3",
				"param4", "param5", "param6", "param7", "param8" };
//		List<DictEntry> dictEntryList = selectDictEntryParam("D00022", "*","zh_CN", paramType);
		//List<DictEntry> dictEntryList = DictCacheUtils.get(paramType,"D00022");
		List<DictEntry> dictEntryList = DictCacheUtils.getDictList("D00022");
		Map<String, Object> columnNames = new HashMap<String, Object>();
		
		for (DictEntry dictEntry : dictEntryList) {
			String dictCode = dictEntry.getDictCode();
			columnNames.put(dictCode.substring(dictCode.indexOf("$") + 1),dictEntry.getDictValue());
		}
		for (int i = 1; i <= columns.length; i++) {
			if (columnNames.containsKey(columns[i - 1])) {
				continue;
			}
			columnNames.put("param" + i, "预留字段"+i);
		}
		return columnNames;
	}

	/**
	 * 获取参数权限
	 * @param orgId 参数所属机构
	 * @return
	 */
	public int getModifyFlag(String orgId) {
		int modifyFlag;
		ParameterInfo rootLevelInfo = selectParamInfo("rootLevel", "*", "*");
		// TODO　UserCacheUtils得到用户的机构级别
		if (StringUtils.equals(rootLevelInfo.getParam1(),SessionContext.getOrgLevel())) {
			modifyFlag = 1;
		} else {
			String sessionOrg = UserCacheUtils.getOrgId();
			if (StringUtils.equals(orgId, sessionOrg)|| orgService.isBelowOrg(orgId, sessionOrg)) {
				modifyFlag = 1;
			} else {
				modifyFlag = 0;
			}
		}

		return modifyFlag;
	}
	/**
	 * 查询客户维护参数
	 * @param sysParameterInfo
	 * @return
	 */
	public List<ParameterInfo> selectKhwh(ParameterInfo sysParameterInfo){
		return parameterDao.selectKhwh(sysParameterInfo);
	}
}